const router = require('koa-router')()
const query = require("../db/mysql");

router.get('/admin',async (ctx,next)=>{
    try{
        let sql = 'SELECT adminId,editP,deleteP FROM admin'
        let data = await query(sql)
        console.log(data)
        return ctx.body = {
            status:200,
            data:data
        }
    }catch{err=>{
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }}
})

router.get('/oneadmin',async (ctx,next)=>{
    let adminId = ctx.query.adminId
    try{
        let sql = 'SELECT adminId,editP,deleteP FROM admin WHERE adminId = ?'
        let data = await query(sql,[adminId])
        // console.log(data)
        return ctx.body = {
            status:200,
            data:data
        }
    }catch{err=>{
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }}
})

router.post('/udadmin',async (ctx,next)=>{
    try{
        const {'管理员id: ':adminId,'管理员密码: ':adminPwd,'编辑权限: ':editP,'删除权限: ':deleteP} = ctx.request.body
        // console.log(ctx.request.body,adminPwd,editP,deleteP,adminId)
        let sql = 'UPDATE admin SET adminPwd = ?,editP = ?,deleteP = ? WHERE adminId = ?'
        await query(sql,[adminPwd,editP,deleteP,adminId])
        return ctx.body = {
            status:200,
            msg:'update success'
        }
    }catch{err=>{
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }}
})

// router.post('/admin',async (ctx,next)=>{
//     try{
//         console.log(ctx.request.body)
//         const {'管理员id: ':adminId,'管理员密码: ':adminPwd,'删除权限: ':deleteP,'编辑权限: ':editP} = ctx.request.body
//         console.log(adminId,adminPwd,editP,deleteP)
//         let sql = 'INSERT INTO admin VALUES(?,?,?,?)'
//         await query(sql,[adminId,adminPwd,editP,deleteP])
//         return ctx.body = {
//             status:200,
//             msg:'insert success'
//         }
//     }catch{err=>{
//         console.log(err)
//         return ctx.body = {
//             status:500,
//             msg:err
//         }
//     }}
// })

router.post('/admin', async (ctx, next) => {
    const {'管理员id: ':adminId,'管理员密码: ':adminPwd,'删除权限: ':deleteP,'编辑权限: ':editP} = ctx.request.body
    try {
        let sql = `insert into admin values(?,?,?,?)`;
        let data = await query(sql,[adminId,adminPwd,editP,deleteP]);
        return ctx.body = {
            status: 200,
            data: '上传成功！',
            msg: ''
        }
    } catch (error) {
        console.log(error);
        return ctx.body = {
            status: 500,
            msg: error
        }
    }
})
router.delete('/admin',async(ctx,next)=>{
    try{
        const adminId = ctx.query.adminId
        console.log(ctx.query.adminId,adminId)
        let sql = 'DELETE FROM admin WHERE adminId = ?'
        await query(sql,[adminId])
        return ctx.body = {
            status:200,
            msg:'delete success'
        }
    }catch{err=>{
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }
    }}
})

module.exports = router